using UnityEngine;
using System.Collections;

public class AudioResource : MonoBehaviour {

	public float fadeSpeed=0.2f;
	
	public AudioClip mainTheme;
	public AudioClip battleTheme;
	public AudioClip victoryTheme;
	public AudioClip lossTheme;

	
	public enum STATE{NONE, MAIN_THEME, BATTLE, VICTORY, LOSS, CHARACTER };
	private STATE state= STATE.NONE;
	
	public AudioSource primary;
	public AudioSource secondary;
	
	
	
	public AudioClip[] attack;
	public AudioClip[] defense;
	public AudioClip[] strength;
	public AudioClip[] speed;
	public AudioClip[] smarts;	
	public AudioSource[] chromosomes;

	public AudioSource once;
	
	void Update() {
		if(state == STATE.NONE) {
			if(primary.volume > 0)
				primary.volume= primary.volume + -fadeSpeed * Time.deltaTime;
			else {
				primary.volume= 0;
				primary.clip= null;
			}
			
			if(secondary.volume > 0)
				secondary.volume= secondary.volume - fadeSpeed * Time.deltaTime;
			else {
				secondary.volume= 0;
				secondary.clip= null;
			}
		}
		else if(state == STATE.CHARACTER ) {
			if(primary.volume > 0)
				primary.volume= primary.volume + -fadeSpeed * Time.deltaTime;
			else {
				primary.volume= 0;
				primary.clip= null;
			}
			
			if(secondary.volume > 0)
				secondary.volume= secondary.volume + -fadeSpeed * Time.deltaTime;
			else {
				secondary.volume= 0;
				secondary.clip= null;
			}
			
			foreach(AudioSource source in chromosomes) {
				//if(source.volume < 1f)
				//	source.volume= source.volume + 0.1f * Time.deltaTime;
				
				float vol = source.volume + fadeSpeed * Time.deltaTime;
					if(vol<1)
						source.volume= vol;
					else
						source.volume=1;
			}
		}
		else {
			if(secondary.clip != null) {
				if(secondary.volume < 1f)
					secondary.volume= secondary.volume + fadeSpeed * Time.deltaTime;
				if(primary.volume > 0)
					primary.volume= primary.volume + -fadeSpeed * Time.deltaTime;
								
				if(secondary.volume == 1f
						&& primary.volume == 0f)
				{
					AudioSource temp= primary;
					primary= secondary;
					secondary= temp;
					secondary.clip= null;
					
					primary.volume= 1f;
					secondary.volume= 0f;
				}
			}	
			foreach(AudioSource source in chromosomes) {
				if(source.volume < 0) {
					float vol = source.volume - fadeSpeed * Time.deltaTime;
					if(vol>0)
						source.volume= vol;
					else
						source.volume=0;
				}
			}
		}
	}
	
	public void play( STATE newState ) {
		if(newState == STATE.NONE) {
			
		}
		else if ( newState == STATE.MAIN_THEME ) {
			secondary.clip= mainTheme;	
			secondary.Play();
		}
		else if ( newState == STATE.BATTLE ) {
			secondary.clip= battleTheme;	
			
			foreach(AudioSource source in chromosomes) {
				source.Stop();
			/*	if(source.volume < 0) {
					float vol = source.volume - fadeSpeed * Time.deltaTime;
					if(vol>0)
						source.volume= vol;
					else
						source.volume=0;
				}
				*/
			}
			
			secondary.Play();
		}
		else if( newState == STATE.CHARACTER ) {
			foreach(AudioSource source in chromosomes) {
				source.volume= 0f;
				source.Play();
			}
		}
		
		state= newState;
	}
	
	private AlienGenome genome=null;
	public void play( AlienGenome genome ) {
		if( this.genome != genome) 
		{
			Debug.Log("playGenome");
			setSourceBy(chromosomes[0], attack, genome.Attack.Max);
			setSourceBy(chromosomes[1], defense, genome.Defense.Max);
			setSourceBy(chromosomes[2], smarts, genome.Smarts.Max);
			setSourceBy(chromosomes[3], strength, genome.Strength.Max);
		
			this.genome= genome;
		}
		play(STATE.CHARACTER);
	}
	
	private void setSourceBy(AudioSource source, AudioClip[] list, int quality) {
		int choice= (int)((quality / 12f) * (float)list.Length);
		
		Debug.Log("setSourceBy: "+ choice);
		
		source.clip= list[choice];
		source.Stop();
		source.Play();
	}

	public void playOnce(STATE state) {
		AudioClip clip= null;
		switch( state) {
			case STATE.VICTORY:
				clip= victoryTheme;
			break;
			case STATE.LOSS:
				clip= lossTheme;
			break;
		}
		
		if(clip != null) {
			once.clip= clip;
			play( STATE.NONE );
			once.Play();
		}
	}
	
}
